﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace Murry.Security
{
    /// <summary>
    /// Class that helps with one-way password encryption.
    /// </summary>
    public static class Password
    {
        /// <summary>
        /// Get a hash for the password.
        /// </summary>
        /// <param name="password">Password to hash.</param>
        /// <returns>Hash of the password.</returns>
        public static string Hash(string password)
        {
            if (String.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException("password");
            }

            var data = HashAsBytes(password);

            var sBuilder = new StringBuilder();

            // convert to hex.
            foreach (var t in data)
            {
                sBuilder.Append(t.ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        /// <summary>
        /// Get has as bytes.
        /// </summary>
        /// <param name="password">Password to hash.</param>
        /// <returns>Hash as bytes.</returns>
        public static byte[] HashAsBytes(string password)
        {
            return SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(password));
        }
    }
}
